import gulp from 'gulp';
import babel from 'gulp-babel';
import uglify from 'gulp-uglify'; //js压缩
import notify from 'gulp-notify'; //提醒
import jshint from 'gulp-jshint'; //js约束
import sass from 'gulp-ruby-sass'; //sass
import autoprefixer from 'gulp-autoprefixer'; //自动适应所有浏览器
import concat from 'gulp-concat'; //合并
import rename from 'gulp-rename'; //重命名
import browserSync from 'browser-sync';
import base64 from 'gulp-base64';
import del from 'del';
import imagemin from 'gulp-imagemin';
import htmlMinify from 'gulp-html-minify';
import runSequence from 'run-sequence';
import amdOptimize from 'amd-optimize';
import minifyCss from 'gulp-minify-css';
import cssBase64 from 'gulp-all-base64';
import rev from 'gulp-rev';
import revCollector from 'gulp-rev-collector';
import browserify from 'browserify';
import standalonify from 'standalonify';
import source from 'vinyl-source-stream';
import buffer from 'vinyl-buffer';
import argv from 'yargs';
import babelify from 'babelify';
import es from 'event-stream';
import glob from 'glob';
import img64 from 'gulp-img64'

const reload = browserSync.reload();


//配置 src目录和 dist目录
const app = {
    srcPath: 'src/',
    distPath: 'dist/',
    amd: false
}

//配置js模块的目录
const paths = {
    'jQuery': `${app.srcPath}lib/js/jquery-1.11.1.min`,

}


//处理html页面
gulp.task('html', () => {
    return gulp.src([`${app.distPath}rev-js/*.json`, `${app.distPath}rev-css/*.json`, `${app.distPath}rev-common-js/*.json`, `${app.srcPath}*.html`, `${app.srcPath}*.xml`])
        .pipe(img64())
        .pipe(revCollector())
        .pipe(htmlMinify())
        .pipe(gulp.dest(`${app.distPath}`))
        .pipe(notify({ message: 'html task complete!' }));
});

//处理js相关内容
gulp.task('jsDone', () => {
    return gulp.src([`${app.srcPath}js/*.*`, `!${app.srcPath}js/main.js`])
        .pipe(babel())
        // .pipe(uglify({
        //     mangle: false, //类型：Boolean 默认：true 是否修改变量名
        //     ie8: true
        // }))
        .pipe(rev())
        .pipe(gulp.dest(`${app.distPath}js/`))
        .pipe(rev.manifest())
        .pipe(gulp.dest(`${app.distPath}rev-common-js`))
        .pipe(notify({ message: 'jsDone task complete!' }));
});

//处理sass样式文件内容
gulp.task('styles', () => {
    return sass([`${app.srcPath}scss/*.scss`, `!${app.srcPath}scss/bootstrap.min.css`, `${app.srcPath}mycss/myHWcss.scss`], { style: 'compressed', sourcemap: true })
        .pipe(autoprefixer({
            browsers: ['last 2 version', 'safari 5', 'ie 8', 'ie 9', 'opera 12.1', 'ios 6', 'android 4'],
            cascade: true, // 美化属性，默认true
            add: true, // 是否添加前缀，默认true
            remove: true, // 删除过时前缀，默认true
            flexbox: true // 为flexbox属性添加前缀，默认true
        }))
        .pipe(base64())
        .pipe(gulp.dest(`${app.distPath}css/`))
        .pipe(minifyCss())
        .pipe(rename({ suffix: '.min' }))
        .pipe(rev()) //- 文件名加MD5后缀
        .pipe(gulp.dest(`${app.distPath}css/`))
        .pipe(rev.manifest()) //生成json文件
        .pipe(gulp.dest(`${app.distPath}rev-css`))
        .pipe(notify({ message: 'sass task complete!' }))
});


//处理第三方包内容，内容平移
gulp.task('lib', () => {
    return gulp.src(`${ app.srcPath }lib/**`)
        .pipe(gulp.dest(`${app.distPath}lib/`))
        .pipe(notify({ message: 'lib task complete!' }));
})


//处理public,内容评议
gulp.task('public', () => {
    return gulp.src(`${ app.srcPath }public/**`)
        .pipe(gulp.dest(`${app.distPath}public/`))
        .pipe(notify({ message: 'public task complete!' }));
})

//处理图片内容，图片压缩后平移
gulp.task('images', () => {
    return gulp.src(`${app.srcPath}images/*.*`)
        .pipe(imagemin())
        .pipe(gulp.dest(`${app.distPath}images/`))
        .pipe(notify({ message: 'images task complete!' }));
});

gulp.task('bundle', () => {
    if (app.amd) {
        return gulp.src([`${app.srcPath}common/*.js`, `${app.srcPath}js/main.js`, `!${app.srcPath}lib/js/jquery-1.11.1.min`, , `${app.srcPath}js/common/*.js`])
            .pipe(babel({
                presets: ['es2015']
            }))
            .pipe(amdOptimize(`${app.srcPath}js/main`, {
                paths: paths
            }))
            .pipe(concat(`main.js`))
            .pipe(babel({
                presets: ['es2015']
            }))
            .pipe(uglify({
                ie8: true
            }))
            .pipe(rev())
            .pipe(gulp.dest(`${app.distPath}js/`))
            .pipe(rev.manifest())
            .pipe(gulp.dest(`${app.distPath}rev-js`))
            .pipe(notify({ message: 'bundle task complete!' }));

    } else {
        return true
    }

});



//删除任务
gulp.task('clean', [], () => {
    return del(['./dist']);
});

//设置browserSync的端口和根目录
gulp.task('browserSync', () => {
    browserSync.init({
        port: 8010,
        server: {
            baseDir: ['./dist']
        }
    });
});

//自动监视 js和css的文件，如果有变化则刷新页面
gulp.task('js-watch', ['bundle', 'jsDone'], browserSync.reload);
gulp.task('bundle-watch', ['bundle'], browserSync.reload);
gulp.task('css-watch', ['styles'], browserSync.reload);
gulp.task('public-watch', ['public'], browserSync.reload);
gulp.task('images-watch', ['images'], browserSync.reload)
    //设置默认任务
gulp.task('default', function() {
    runSequence('clean', 'images', ['styles', 'jsDone', 'lib', 'public', 'bundle'], 'html', 'browserSync')

    gulp.watch('src/js/*.js', function() {
        runSequence(['jsDone'], 'html');
    });

    gulp.watch('src/common/*.js', function() {
        runSequence(['bundle'], 'html');
    });
    gulp.watch('src/scss/*.scss', function() {
        runSequence(['styles'], 'html')
    });
    gulp.watch('src/mycss/**', function() {
        runSequence(['styles'], 'html')
    });
    gulp.watch('src/lib/**', ['lib']);
    gulp.watch('src/public/**', ['public']);
    gulp.watch('src/public/**', ['public-watch']);

    gulp.watch('src/images/*.*', ['images']);
    gulp.watch(["src/*.html"]).on("change", browserSync.reload);
    gulp.watch(['src/common/*.js'], ['bundle-watch']);
    gulp.watch(['src/js/*.js'], ['js-watch']);
    gulp.watch(['src/scss/*.scss'], ['css-watch']);
    gulp.watch('src/images/*.*', ['images-watch']);
    gulp.watch('src/*.html', ['html']);
});